import axios from 'axios';

// 约定前后端交互的格式：{errno, data, msg}
export type ResType = {
    code: number,
    success?: boolean,
    data?: ResDataType,
    message?: string,
}

export type ResDataType = {
    [key: string]: any
}

// 配置axios实例
const instance = axios.create({
    baseURL: import.meta.env.VITE_BASE_API,
    timeout: 5000,
});

// 配置响应拦截器
instance.interceptors.response.use((res) => {
    const { success, message, data } = (res.data || {}) as ResType;
    if (success) {
        return data as any;
    }
    return Promise.reject(new Error(message));
});

export default instance;
